<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>侯文鹏的 AI</title>

    <!--页面样式-->
    <style>
      body {
            font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;
            background: linear-gradient(135deg, #56ccf2, #2f80ed);
            color: #fff;
            margin: 0;
            padding: 0;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            box-sizing: border-box;
        }

        .container {
            width: 100%;
            max-width: 900px;
            background: rgba(255, 255, 255, 0.1);
            padding: 40px;
            border-radius: 40px;
            box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
            backdrop-filter: blur(15px);
            box-sizing: border-box;
            text-align: center;
        }

        h1 {
            font-size: 3.8rem;
            color: #2ecc71;
            font-weight: 800;
            letter-spacing: 3px;
            text-shadow: 2px 2px 6px rgba(0, 0, 0, 0.3);
            margin-bottom: 20px;
        }

        h4, h6 {
            color: #bdc3c7;
            margin-bottom: 15px;
            font-weight: 400;
            font-size: 1.1rem;
        }

        h4 {
            color: #ff9f43;
            font-size: 1.4rem;
            text-transform: uppercase;
            letter-spacing: 2px;
        }

        h6 {
            color: #f39c12;
            font-size: 1.2rem;
            font-weight: 600;
        }

        form {
            display: flex;
            justify-content: center;
            gap: 20px;
            margin-top: 20px;
            align-items: center;
            flex-wrap: wrap;
        }

        input[type="text"] {
            width: 93%;
            padding: 16px;
            border: 2px solid #34495e;
            border-radius: 50px;
            font-size: 1.1rem;
            background-color: #34495e;
            color: #fff;
            transition: all 0.3s ease;
            outline: none;
            box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
        }

        input[type="text"]:focus {
            border-color: #2ecc71;
            box-shadow: 0 0 12px rgba(46, 204, 113, 0.7);
        }

        input[type="submit"] {
            padding: 14px 40px;
            background-color: #2ecc71;
            border: none;
            color: #fff;
            border-radius: 50px;
            font-size: 1.3rem;
            cursor: pointer;
            transition: all 0.3s ease;
            box-shadow: 0 4px 10px rgba(0, 0, 0, 0.1);
        }

        input[type="submit"]:hover {
            background-color: #27ae60;
            transform: translateY(-3px);
        }

        /* 下拉框样式 */
        select {
            padding: 12px 20px;
            background-color: #34495e;
            color: #fff;
            border-radius: 50px;
            margin-top: 15px;
            font-size: 1.1rem;
            border: 2px solid #34495e;
            cursor: pointer;
            transition: all 0.3s ease;
            box-shadow: 0 4px 10px rgba(0, 0, 0, 0.2);
        }

        select:hover, select:focus {
            border-color: #2ecc71;
            box-shadow: 0 0 12px rgba(46, 204, 113, 0.7);
        }

        /* 文件上传样式 */
        input[type="file"] {
            padding: 12px 20px;
            background-color: #34495e;
            color: #fff;
            border-radius: 50px;
            cursor: pointer;
            margin-top: 15px;
            font-size: 1.1rem;
            transition: all 0.3s ease;
            box-shadow: 0 4px 10px rgba(0, 0, 0, 0.2);
        }

        input[type="file"]:hover, input[type="file"]:focus {
            background-color: #2ecc71;
            box-shadow: 0 0 12px rgba(46, 204, 113, 0.7);
        }

        .chat-box {
            width: 100%;
            max-height: 500px;
            min-height: 150px;
            overflow-y: auto;
            margin-top: 30px;
            background-color: #34495e;
            border-radius: 30px;
            padding: 20px;
            box-shadow: inset 0 0 20px rgba(0, 0, 0, 0.3);
            animation: fadeIn 1s ease;
            scrollbar-width: thin;
            scrollbar-color: #2ecc71 #34495e;
        }

        .chat-box::-webkit-scrollbar {
            width: 8px;
        }

        .chat-box::-webkit-scrollbar-thumb {
            background-color: #2ecc71;
            border-radius: 10px;
        }

        .chat-box::-webkit-scrollbar-track {
            background: #34495e;
            border-radius: 10px;
        }

        .message {
            padding: 14px 18px;
            margin-bottom: 15px;
            border-radius: 20px;
            word-wrap: break-word;
            max-width: 80%;
            line-height: 1.6;
            font-size: 1.1rem;
            position: relative;
            transition: all 0.2s ease-in-out;
            animation: slideIn 0.5s ease;
            box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
        }

        .user-message {
            background-color: #2ecc71;
            color: #fff;
            margin-left: auto;
            border-radius: 25px 25px 0 25px;
            animation: userMessageIn 0.5s ease-out;
        }

        .bot-message {
            background-color: #e67e22;
            color: #fff;
            border-radius: 25px 25px 25px 0;
            animation: botMessageIn 0.5s ease-out;
        }

        .message-text {
            display: inline-block;
            word-wrap: break-word;
            max-width: 100%;
        }

        .loader {
            text-align: center;
            margin-top: 20px;
        }

        .loader::after {
            content: "";
            display: inline-block;
            width: 35px;
            height: 35px;
            border-radius: 50%;
            border: 4px solid #ccc;
            border-top-color: #2ecc71;
            animation: spin 1s infinite linear;
        }

        @keyframes spin {
            to {
                transform: rotate(360deg);
            }
        }

        @keyframes fadeIn {
            0% {
                opacity: 0;
            }

            100% {
                opacity: 1;
            }
        }

        @keyframes slideIn {
            0% {
                opacity: 0;
                transform: translateX(-50px);
            }

            100% {
                opacity: 1;
                transform: translateX(0);
            }
        }

        @keyframes userMessageIn {
            0% {
                opacity: 0;
                transform: scale(0.9);
            }

            100% {
                opacity: 1;
                transform: scale(1);
            }
        }

        @keyframes botMessageIn {
            0% {
                opacity: 0;
                transform: scale(0.9);
            }

            100% {
                opacity: 1;
                transform: scale(1);
            }
        }
    </style>

</head>

<body>

<!--页面div布局-->
<div class="container">
    <h4 style="color: #fff;">AI搭建者: <span style="color: #fff;">侯文鹏</span></h4>
    <h6 style="color: #fff;">搭建日期: <span style="color: #fff;">2025</span></h6>
    <h1>AI</h1>

    <form id="form" action="/upload" method="post" enctype="multipart/form-data">

        <!--文本框-->
        <input type="text" id="message" name="message" placeholder="请输入你的问题..." required>

        <!--选择类型-->
        <select id="ai-type" name="ai-type">
            <option value="normal-chat">AI - 聊天</option>
            <option value="prompt-to-image">AI - 根据问题生成图片</option>
            <option value="image-to-text">AI - 解析上传的图片内容</option>
            <option value="video-to-text">AI - 解析上传的视频内容</option>
        </select>

        <!--文件上传-->
        <input type="file" id="file-upload" name="file-upload" accept="image/*,video/*,audio/*">

        <!--提交按钮-->
        <input type="submit" value="发送">
    </form>

    <div id="loader" class="loader" style="display: none;"></div>
    <div id="chat-box" class="chat-box"></div>
</div>

<!--触发事件-->
<script type="module">
    // 引入三方js
    import { marked } from "https://cdn.jsdelivr.net/npm/marked/lib/marked.esm.min.js";

    var loader = document.getElementById("loader");

    // 监听表单提交事件
    document.getElementById("form").addEventListener("submit", function(event) {

        event.preventDefault();

        // --------- 页面内容获取 ----------

        // 文本框内容
        var messageInput = document.getElementById("message");
        var message = messageInput.value.trim();

        // 选择类型
        var aiType = document.getElementById("ai-type").value;

        // 文件上传的内容
        var fileInput = document.getElementById("file-upload");
        var file = fileInput.files[0];

        // 防止发送空消息
        if (message === "") {
            alert("请输入消息！");
            return;
        }

        messageInput.value = "";

        var chatBox = document.getElementById("chat-box");

        // 创建用户消息并显示
        var userMessage = document.createElement("div");
        userMessage.className = "message user-message";
        userMessage.textContent = " " + message;
        chatBox.appendChild(userMessage);
        chatBox.scrollTop = chatBox.scrollHeight;

        // 显示加载动画
        loader.style.display = "block";

        // ---------- 组装给后台接口的请求参数 ----------
        var formData = new FormData();

        // 根据图片生成文字
        if(aiType === "image-to-text"){
            formData.append("message", message);
            formData.append("file", file);
        }

        // 根据视频生成文字
        if(aiType === "video-to-text"){
            formData.append("message", message);
            formData.append("file", file);
        }


        // ------------ 请求后端接口 --------------- localhost  120.26.243.41
        var xhr = new XMLHttpRequest();
        // 正常聊天
        if(aiType === "normal-chat"){
            xhr.open("GET", "http://localhost:9051/ai/chat?message=" + encodeURIComponent(message), true);
        }

         // 根据提示生成图片
         if(aiType === "prompt-to-image"){
            xhr.open("GET", "http://localhost:9051/ai/image/genImage?input="+ encodeURIComponent(message), true);
        }

        // 根据视频生成文字
        if(aiType === "video-to-text"){
            xhr.open("POST", "http://localhost:9051/ai/video/video", true);
        }

        // 根据图片生成文字
        if(aiType === "image-to-text"){
            xhr.open("POST", "http://localhost:9051/ai/tmageTotext/image", true);
        }

     xhr.onreadystatechange = function() {
            if (xhr.readyState === 4) {
                loader.style.display = "none";  // 隐藏加载动画

                if (xhr.status === 200) {
                    var response = xhr.responseText;

                    console.log("AI Response:", response);  // 输出响应，检查是否是有效的图片 URL

                    if (aiType === "prompt-to-image") {
                        // 检查返回的图片 URL
                        var img = document.createElement("img");
                        img.src = response;  // 直接使用返回的图片链接
                        img.alt = "Generated Image";  // 给图片设置 alt 属性
                        img.style.maxWidth = "100%";  // 控制图片最大宽度适应页面
                        img.style.borderRadius = "15px";  // 圆角效果

                        // 创建机器人消息并显示
                        var botMessage = document.createElement("div");
                        botMessage.className = "message bot-message";

                        // 将图片插入到消息框中
                        botMessage.appendChild(img);

                        chatBox.appendChild(botMessage);
                        chatBox.scrollTop = chatBox.scrollHeight;
                    } else {
                        // 正常聊天处理
                        var botMessage = document.createElement("div");
                        botMessage.className = "message bot-message";

                        // 使用 marked 解析 Markdown 内容
                        var botMessageText = document.createElement("span");
                        botMessageText.className = "message-text";
                        botMessage.appendChild(botMessageText);
                        botMessageText.innerHTML = marked(response);  // 使用 marked 解析 Markdown 内容

                        chatBox.appendChild(botMessage);
                        chatBox.scrollTop = chatBox.scrollHeight;
                    }
                } else if (xhr.status === 400) {
                    var error = JSON.parse(xhr.responseText);
                    var errorMessage = document.createElement("div");
                    errorMessage.className = "message bot-message";
                    errorMessage.textContent = "Bot: " + error.message;
                    chatBox.appendChild(errorMessage);
                    chatBox.scrollTop = chatBox.scrollHeight;
                } else {
                    var errorMessage = document.createElement("div");
                    errorMessage.className = "message bot-message";
                    errorMessage.textContent = "Bot: 无法连接到后端服务。请确保后端服务正在运行。";
                    chatBox.appendChild(errorMessage);
                    chatBox.scrollTop = chatBox.scrollHeight;
                }
            }
        };



        // 发送请求
        xhr.send(formData);
    });
</script>

</body>
</html>